package com.citycloud.ccuap.tc.admin.projecttype;

import com.citycloud.ccuap.commons.sys.constant.SysConstant;
import com.citycloud.ccuap.commons.uuid.UUIDGenerator;
import com.citycloud.ccuap.framework.exception.AppAssert;
import com.citycloud.ccuap.framework.service.mybatis.MybatisServiceImpl;
import com.citycloud.ccuap.tc.admin.constant.AdminConstant;
import com.citycloud.ccuap.tc.admin.entity.SysProjectType;
import com.citycloud.ccuap.tc.admin.repository.SysProjectTypeRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 * @Description 站点类型业务逻辑处理
 * @Date 2019/12/19 9:50 AM
 * @Author liuyan
 **/
@Service
public class ProjectTypeService extends MybatisServiceImpl<SysProjectTypeRepository, SysProjectType, String> implements IProjectTypeService{

	private static final String PROJECT_TYPE_CODE_PREFIX="project_type_";
	
	@Autowired
	private SysProjectTypeRepository projectTypeRepository;

	/**
	 * 查询所有项目类型
	 * @param sort
	 * @return
	 * 创建日期：Dec 19, 2019
	 * 修改说明：
	 * @author liuyan
	 */
	public List<SysProjectType> getAll(Sort sort){
		return projectTypeRepository.findAll(sort);
	}
	
	/**
	 * 新增站点类型
	 * @see MybatisServiceImpl#insert(com.citycloud.ccuap.framework.service.Model)
	 * @param projectType
	 * @return
	 * 创建日期：Dec 19, 2019
	 * 修改说明：
	 * @author liuyan
	 */
	@Override
    public SysProjectType insert(SysProjectType projectType) {
		AppAssert.isTrue(existName(projectType.getName()),"站点名称已存在!");
		projectType.setCreateTime(new Date());
		return projectTypeRepository.insert(projectType);
	}

	@Override
	public SysProjectType create(SysProjectType type) {
		type.setId(null);
		AppAssert.hasText(type.getName(),"站点类型名称不能为空！");
		AppAssert.isTrue(existName(type.getName()),"站点名称已存在!");
		type.setCreateTime(new Date());
		if (SysConstant.MODULE_CODE_GENERATE_RULE.equalsIgnoreCase(type.getCode())) {
			type.setCode(PROJECT_TYPE_CODE_PREFIX + UUIDGenerator.nextId());
		}
		type.setSeq(type.getSeq() == null ? 0 : type.getSeq());
		SysProjectType retProjectType = projectTypeRepository.save(type);
		return retProjectType;
	}

	@Override
	public SysProjectType modify(SysProjectType type) {
		AppAssert.hasText(type.getId(),"站点类型ID不能为空！");
		AppAssert.hasText(type.getName(),"站点类型名称不能为空！");
		SysProjectType projectType = findById(type.getId());
		AppAssert.notNull(projectType,"该站点不存在！");
		if(!projectType.getName().equals(type.getName())){
			AppAssert.isTrue(existName(type.getName()),"站点名称已存在!");
		}
		//修改字段
		projectType.setUpdateTime(new Date());
		projectType.setDescription(type.getDescription());
		//名称唯一性
		projectType.setName(type.getName());
		projectType.setIcon(type.getIcon());
		projectType.setSeq(type.getSeq() == null ? 0 : type.getSeq());
		return update(projectType);
	}

	@Override
	public List<SysProjectType> findByCode(String code) {
		return projectTypeRepository.findByCode(code);
	}

	/**
	 * 校验名称是否存在
	 * @param name 站点类型名称
	 * @return
	 * @author liuyan
	 */
	public boolean existName(String name){
        List<SysProjectType> types = projectTypeRepository.findByName(name);
		return CollectionUtils.isEmpty(types);
	}



}
